package com.fire.eyes.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

/**
 *
 * @author Aspire
 * @date 2018-04-18
 */
public class DateUtil {
	
		/**
	 * 日期格式，年月日，用横杠分开，例如：2006-12-25，2008-08-08
	 */
	public static final String DATE_FORMAT_YYYY_MM_DD = "yyyy-MM-dd";

	/**
	 * 日期格式，年月日时分秒，年月日用横杠分开，时分秒用冒号分开 例如：2005-05-10 23：20：00，2008-08-08 20:08:08
	 */
	public static final String DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS = "yyyy-MM-dd HH:mm:ss";
	
	/**
     * 时间格式，时分秒用冒号分开 例如：23：20：00，20:08:08
     */
    public static final String DATE_TIME_FORMAT_HH_MI_SS = "HH:mm:ss";

    private static final Integer DATE_FINAL = 29;
	/**
	 * 获取两个日期间的年数
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int getYears(String date1, String date2) {
		Date dateObj1 = null;
		Date dateObj2 = null;
		try {
			dateObj1 = DateUtils.parseDate(date1, DATE_FORMAT_YYYY_MM_DD);
			dateObj2 = StringUtils.isBlank(date2) ? new Date() : DateUtils
					.parseDate(date2, DATE_FORMAT_YYYY_MM_DD);
		} catch (ParseException e) {
			throw new IllegalArgumentException("日期格式错误，必须传入yyyy-MM-dd的日期字符串");
		}

		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c1.setTime(dateObj1);
		c2.setTime(dateObj2);
		/*
		 * if (c2.get(Calendar.MONTH) == 1 && c2.get(Calendar.DATE) == 28) { //
		 * date2是2月28号 if (isLeapYear(c2) && !isLeapYear(c1)) { //
		 * date1不是闰年，date2是闰年 c2.add(Calendar.DATE, -1); } }
		 */
		if (c1.get(Calendar.MONTH) == 1 && c1.get(Calendar.DATE) == DATE_FINAL) {
			// date1是2月29号
			if (!isLeapYear(c2) && isLeapYear(c1)) {
				// date1是闰年，date2不是闰年
				c1.add(Calendar.DATE, -1);
			}
		}
		int yearNow = c2.get(Calendar.YEAR);
		int monthNow = c2.get(Calendar.MONTH);
		int dayOfMonthNow = c2.get(Calendar.DAY_OF_MONTH);

		int yearBirth = c1.get(Calendar.YEAR);
		int monthBirth = c1.get(Calendar.MONTH);
		int dayOfMonthBirth = c1.get(Calendar.DAY_OF_MONTH);
		int age = yearNow - yearBirth;
		if (monthNow <= monthBirth) {
			if (monthNow == monthBirth) {
				if (dayOfMonthNow < dayOfMonthBirth) {
					age--;
				}

			} else {
				age--;
			}
		}
		return age;
	}

	/**
	 * 获取两个日期间的年数
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int getDays(String date1, String date2) {
		Date dateObj1 = null;
		Date dateObj2 = null;
		try {
			dateObj1 = DateUtils.parseDate(date1, DATE_FORMAT_YYYY_MM_DD);
			if (StringUtils.isBlank(date2)) {
				date2 = DateFormatUtils.format(new Date(), DATE_FORMAT_YYYY_MM_DD);
			}
			dateObj2 = DateUtils.parseDate(date2, DATE_FORMAT_YYYY_MM_DD);
		} catch (ParseException e) {
			throw new IllegalArgumentException("日期格式错误，必须传入yyyy-MM-dd的日期字符串");
		}

		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c1.setTime(dateObj1);
		c2.setTime(dateObj2);

		int days = 0;
		while (c2.after(c1)) {
			// 循环对比，直到相等
			days++;
			c1.add(Calendar.DATE, 1);
		}
		return days;
	}

	/**
	 * 是否闰年
	 * 
	 * @return
	 */
	private static boolean isLeapYear(Calendar calendar) {
		int year = calendar.get(Calendar.YEAR);
		return year % 100 == 0 ? year % 400 == 0 : year % 4 == 0;
	}

	/**
	 * 获取现在时间
	 * 
	 * @return 返回短时间字符串格式yyyy-MM-dd
	 */
	public static String getFormatDate(Date dt) {
		// Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD);
		return formatter.format(dt);
	}

	public static String dateToString(Date date, String pattern) {
		if (date == null) {
			return null;
		}
		try {
			SimpleDateFormat sfDate = new SimpleDateFormat(pattern);
			sfDate.setLenient(false);
			return sfDate.format(date);
		} catch (Exception e) {
			return null;
		}
	}

	public static Date stringToDate(String dateText, String format,
			boolean lenient) {
		if (dateText == null) {
			return null;
		}
		DateFormat df = null;
		try {
			if (format == null) {
				df = new SimpleDateFormat();
			} else {
				df = new SimpleDateFormat(format);
			}
			// setLenient avoids allowing dates like 9/32/2001
			// which would otherwise parse to 10/2/2001
			df.setLenient(false);
			return df.parse(dateText);
		} catch (ParseException e) {
			return null;
		}
	}

	public static boolean isBelong(String strBeginTime, String strEndTime) throws Exception {
		// 设置日期格式
		SimpleDateFormat df = new SimpleDateFormat("HH:mm");
		Date now = null;
		Date beginTime = null;
		Date endTime = null;
		try {
			now = df.parse(df.format(new Date()));
			// "00:00"
			beginTime = df.parse(strBeginTime);
			// "10:00"
			endTime = df.parse(strEndTime);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(now == null || beginTime == null || endTime == null) {
			throw new Exception();
		}
		return belongCalendar(now, beginTime, endTime);
	}

	/**
	 * 判断时间是否在时间段内
	 * 
	 * @param nowTime
	 * @param beginTime
	 * @param endTime
	 * @return
	 */
	public static boolean belongCalendar(Date nowTime, Date beginTime,
			Date endTime) {
		Calendar date = Calendar.getInstance();
		date.setTime(nowTime);

		Calendar begin = Calendar.getInstance();
		begin.setTime(beginTime);

		Calendar end = Calendar.getInstance();
		end.setTime(endTime);

		if (date.after(begin) && date.before(end)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 获取当前时间
	 * 
	 * @param pattern
	 * @return
	 */
	public static String getCurrentDateString(String pattern) {
		return dateToString(getCurrentDateTime(), pattern);
	}

	/**
	 * 获取当前时间
	 * 
	 * @return
	 */
	public static Date getCurrentDateTime() {
		Calendar calNow = Calendar.getInstance();
		Date dtNow = calNow.getTime();
		return dtNow;
	}

	/**
	 * 比较两个时间
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int compareDate(String date1, String date2) {
		DateFormat df = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
		try {
			Date dt1 = df.parse(date1);
			if(date2 == null){
				date2 = getCurrentDateString(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
			}
			Date dt2 = df.parse(date2);
			if (dt1.getTime() > dt2.getTime()) {
				return 1;
			} else if (dt1.getTime() < dt2.getTime()) {
				return -1;
			} else {
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;
	}
	
	/**
     * 格式化时间
     */
    public static String getFormatDate(Date date, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(date);
    }
    
	/*public static void main(String[] args){
		System.out.println(compareDate("2017-12-11 00:00:00", null));
	}*/
}
